﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Collections.Specialized;


namespace production.Controls
{
    public class prodTaskTemplateOutGrid : commonV2.controls.baseDataGridView
    {
        public enum ColumnNames
        {
            taskCode, itemCode, itemName, itemUnit, qty, qtyWeight, amtWeight
        };

        public prodTaskTemplateOutGrid()
        {
            this.myOnFind += new OnCellEvent(FindData);
        }

        private data.masterDS.ItemDataTable myItemTbl = new data.masterDS.ItemDataTable();

        private void UpdateItemList(string itemCode)
        {
            if (itemCode == null)
            {
                data.productionDS.prodTaskTemplateOutDataTable dataTbl = (data.productionDS.prodTaskTemplateOutDataTable)myEditDataSource.DataSource;
                for (int idx = 0; idx < dataTbl.Count; idx++)
                {
                    if (dataTbl[idx].RowState == DataRowState.Deleted) continue;
                    DataAccess.DataCache.Find(myItemTbl, dataTbl[idx].itemCode);
                }
            }
            else DataAccess.DataCache.Find(myItemTbl, itemCode);
        }
        
        public override void Init()
        {
            base.Init();
            data.productionDS.prodTaskTemplateOutDataTable dummyProdTaskTemplateTbl = new data.productionDS.prodTaskTemplateOutDataTable();
            DataGridViewCellStyle dataGridViewCellQtyType = new DataGridViewCellStyle();
            dataGridViewCellQtyType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            dataGridViewCellQtyType.Format = CommonTypes.Settings.sysMaskQty;
            dataGridViewCellQtyType.NullValue = null;

            DataGridViewCellStyle dataGridViewCellCenterType = new DataGridViewCellStyle();
            dataGridViewCellCenterType.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

            // 
            // taskCode
            // 
            commonV2.controls.DataGridViewTextBoxColumnExt taskCode = new commonV2.controls.DataGridViewTextBoxColumnExt();
            taskCode.Uppercase = true;
            taskCode.DataPropertyName = dummyProdTaskTemplateTbl.taskCodeColumn.ColumnName;
            taskCode.HeaderText = "Mã số";
            taskCode.Name = ColumnNames.taskCode.ToString();
            taskCode.Width = 130;
            taskCode.Visible = false;

            // 
            // itemCode
            // 
            commonV2.controls.DataGridViewTextBoxColumnExt itemCode = new commonV2.controls.DataGridViewTextBoxColumnExt();
            itemCode.Uppercase = true;
            itemCode.DataPropertyName = dummyProdTaskTemplateTbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã.hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 130;
            // 
            // itemName
            // 
            BindingSource itemSrc = new BindingSource();
            itemSrc.DataSource = myItemTbl;

            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataSource = itemSrc;
            itemName.DataPropertyName = dummyProdTaskTemplateTbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = myItemTbl.nameColumn.ColumnName;
            itemName.ValueMember = myItemTbl.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;
            itemName.Width = 250;
            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

            commonV2.ColorSet csDisable = commonV2.ThemeLibs.GetColorSetINFO();
            itemName.DefaultCellStyle.BackColor = csDisable.BackColor;
            itemName.DefaultCellStyle.ForeColor = csDisable.ForeColor;

            // 
            // itemUnit
            // 
            DataGridViewComboBoxColumn itemUnit = new DataGridViewComboBoxColumn();
            itemUnit.DataSource = itemSrc;
            itemUnit.DataPropertyName = dummyProdTaskTemplateTbl.itemCodeColumn.ColumnName;
            itemUnit.DisplayMember = myItemTbl.unitColumn.ColumnName;
            itemUnit.ValueMember = myItemTbl.itemCodeColumn.ColumnName;
            itemUnit.HeaderText = "ĐVT";
            itemUnit.Name = ColumnNames.itemUnit.ToString();
            itemUnit.ReadOnly = true;
            itemUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

            if (csDisable != null)
            {
                itemUnit.DefaultCellStyle.BackColor = csDisable.BackColor;
                itemUnit.DefaultCellStyle.ForeColor = csDisable.ForeColor;
            }
            itemUnit.Width = 50;
            
            // 
            // qty 
            // 
            DataGridViewTextBoxColumn qty = new DataGridViewTextBoxColumn();
            qty.DataPropertyName = dummyProdTaskTemplateTbl.qtyColumn.ColumnName;
            qty.DefaultCellStyle = dataGridViewCellQtyType;
            qty.HeaderText = "Lượng";
            qty.Name = ColumnNames.qty.ToString();
            qty.Width = 50;
            qty.Visible = true;

            // 
            // qtyWeight 
            // 
            DataGridViewTextBoxColumn qtyWeight = new DataGridViewTextBoxColumn();
            qtyWeight.DataPropertyName = dummyProdTaskTemplateTbl.qtyWeightColumn.ColumnName;
            qtyWeight.DefaultCellStyle = dataGridViewCellCenterType;
            qtyWeight.HeaderText = "TS1";
            qtyWeight.Name = ColumnNames.qtyWeight.ToString();
            qtyWeight.Width = 40;
            qtyWeight.Visible = true;

            // 
            // amtWeight 
            // 
            DataGridViewTextBoxColumn amtWeight = new DataGridViewTextBoxColumn();
            amtWeight.DataPropertyName = dummyProdTaskTemplateTbl.amtWeightColumn.ColumnName;
            amtWeight.DefaultCellStyle = dataGridViewCellCenterType;
            amtWeight.HeaderText = "TS2";
            amtWeight.Name = ColumnNames.amtWeight.ToString();
            amtWeight.Width = 40;
            amtWeight.Visible = true;

            // 
            // detailGrid
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new DataGridViewColumn[] {
                    taskCode, itemCode, itemName,itemUnit,qty,qtyWeight,amtWeight
                });
            this.DisableReadOnlyColumns();
        }

        protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
        {
            base.OnCellValueChanged(e);
            if (this.CurrentCell == null || !this.IsValidCell(e.RowIndex, e.ColumnIndex)) return;
            try
            {
                if (this.IsOnProcessing) return;
                this.IsOnProcessing = true;

                if (this.Columns[e.ColumnIndex].Name == ColumnNames.itemCode.ToString())
                {
                    UpdateItemList(this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
                this.IsOnProcessing = false;
            }
            catch (Exception er)
            {
                this.IsOnProcessing = false;
                this.ErrorHandler(this,er);
            }
        }
        public override void Refresh()
        {
            if (IsOnProcessing) return;
            try
            {
                IsOnProcessing = true;
                UpdateItemList(null);
                IsOnProcessing = false;
                base.Refresh();
            }
            catch (Exception er)
            {
                IsOnProcessing = false;
                ErrorHandler(this, er);
            }
        }

        private void FindData(object sender, commonV2.controls.GridEventArgs e)
        {
            FindData((DataGridView)sender, e.RowIndex, e.ColumnIndex);
        }
        private void FindData(DataGridView grid, int rowIndex, int columnIndex)
        {
            string findCode = grid.Rows[rowIndex].Cells[columnIndex].EditedFormattedValue.ToString(); ;
            if (grid.CurrentCell == null) return;
            //Code
            if (grid.Columns[columnIndex].Name.Trim() == ColumnNames.itemCode.ToString())
            {
                application.dataFind.myItemFind.Find(findCode, true);
                data.masterDS.ItemRow itemRow = application.dataFind.myItemFind.selectedDataRow;
                if (itemRow != null)
                    grid.Rows[rowIndex].Cells[columnIndex].Value = itemRow.itemCode;
                else grid.Rows[rowIndex].Cells[columnIndex].Value = DBNull.Value;
                this.myEditDataSource.ResetBindings(false);
                this.EndEdit();
            }
        }
    }
}
